import java.util.*;

/**
 * @Author Stringzhua
 * @Date 2024/11/30 14:26
 * description:
 */
public class demo04 {
    public static void main(String[] args) {
        //这里有两个数组,需要你求并集并去重
        int[] a = {1, 2, 3, 4, 5, 6};
        int[] b = {4, 5, 6, 7, 8, 9, 10, 11};
        int[] result = unionArray(a, b);
        for (int num : result) {
            System.out.print(num + " ");
        }
    }

    private static int[] unionArray(int[] a, int[] b) {
        //判空
        if (a == null) {
            a = new int[0];
        }
        if (b == null) {
            b = new int[0];
        }
        List<Integer> list = new ArrayList<>();
        for (int num : a) {
            list.add(num);
        }
        for (int num : b) {
            list.add(num);
        }

        //数组去重和排序
        Set<Integer> set = new TreeSet<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1 - o2;
            }
        });
        set.addAll(list);
        System.out.println(set);

        //将结果存入数组中返回
        int[] res = new int[set.size()];
        int index = 0;
        for (Integer num : set) {
            res[index++] = num;
        }
        return res;
    }
}
